package kjmvc;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 public abstract class KJControl extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
	private static final long serialVersionUID = 1L;
	
	protected String DEFAULT_URI = "/index.jsp";
	public KJControl() {
		super();
	}   	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		doPost(request, response);
	}  	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		if(request.getParameter("action") == null) {
			PrintWriter out = response.getWriter();
			out.println("action is null");
			return;
		}
		
		String forward = DEFAULT_URI;
		try {
			forward = doControl(request, response);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
//		forwardView(forward, request, response);
		redirectView(forward, response);
	}   	  	
	
	protected abstract String doControl(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException, SQLException;
	
	protected void forwardView(String forward, HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
//		getServletContext().getRequestDispatcher(forward).forward(request, response);
		RequestDispatcher dispatcher = request.getRequestDispatcher(forward);
		dispatcher.forward(request, response);
	}
	protected void redirectView(String forward, HttpServletResponse response)
	throws IOException {
		response.sendRedirect(forward);
	}
}